// pages/detail-video/index.js
const db = wx.cloud.database()
import {userStore} from '../../store/index'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
Page({

  data: {
    mvURLInfo:{},
    mvDetail:{},
    topMVs:{},
    mvId:0,
    commentList:[],
    commentValue:'',
    haveComment:false,
    isLogin:false
  },

  onLoad: function (options) {
    const id = options.id
    this.setData({
      mvId:id
    })
    this.getLogin()
    this.getPageData(id)
    this.getComment()
  },

  onShow: function () {
    this.getComment()
  },

  getLogin:function(){
    userStore.onState("isLogin",(value)=>{
      console.log(value)
      this.setData({isLogin:value})
    })
  },

  // ========================视频播放完毕======================
  videoEnd:function(){
    const _ = db.command
    db.collection('topMVs').doc(this.data.topMVs._id)
    .update({
      data:{
        playCount:_.inc(1)
      }
    })

    db.collection('mvDetail').doc(this.data.mvDetail._id)
    .update({
      data:{
        playCount:_.inc(1)
      },
    })

    this.getPageData(this.data.mvId)
  },

  //==========================点击评论===========================
  getComment:function(){
    this.setData({commentList:[]})
    var that = this
   
    db.collection('mvComment').where({
      mvId:this.data.mvId * 1
    })
    .get().then(res =>{
     
      if(res.data.length == 0){
        this.setData({haveComment:false})
        return
      }else if(res.data[0].comment){
        console.log(res);
        this.setData({commentList:res.data[0].comment})
        this.setData({haveComment:true})
        this.setData({commentId:res.data[0]._id})
      }
      
    })

  },
  
  // =======================填写评论赋值==========================
  onChangeComment:function(event){
    console.log(event.detail)
    this.setData({commentValue:event.detail})
  },
  // =======================点击评论框没有登陆则跳转==========================
  handleToLoginClick:function(){
    userStore.onState("isLogin",(value)=>{
      console.log(value)
      this.setData({isLogin:value})
    })
    if(this.data.isLogin == "false"){
      Dialog.confirm({
        // title:'标题',
        message: '您还未登录，是否前往登录？',
      })
        .then(() => {
          wx.navigateTo({
            url: '../login/index',
          })
        })
        .catch(() => {
          console.log('取消')
        });
    }
  },

  //=======================点击发表按钮===============================
  handleMakeCommentBtnClick:function(){
    const comment = this.data.commentValue
    if(!comment){
      Dialog.alert({
        // title:"评论不能为空",
        message:"评论不能为空"
      })
      return;
    }
    userStore.onState("nickname",(value)=>{
      console.log(value)
      this.setData({nickname:value})
    })
    userStore.onState("avatarUrl",(value)=>{
      console.log(value)
      this.setData({avatarUrl:value})
    })
    //本来歌曲没有评论的
    if(this.data.haveComment == false){
      db.collection('mvComment').add({
        data:{
          mvId:this.data.mvId * 1,
          comment:[{
            content:comment,
            userName:this.data.nickname,
            userPic:this.data.avatarUrl
          }]
        }
      }).then(res=>{
        Toast('发表成功');
        this.getComment()
        this.setData({commentValue:""})
      })
      
    }
    //本来歌曲有评论的
    if(this.data.haveComment == true){
      const _ = db.command
      db.collection('mvComment').doc(this.data.commentId).update({
        data:{
          comment: _.push({
            content:comment,
            userName:this.data.nickname,
            userPic:this.data.avatarUrl
          })
        }
      })
      .then(res=>{
        Toast('发表成功');
        this.getComment()
        this.setData({commentValue:""})
      })
      
    }
      
  },

  getPageData(id){
    var that = this

    //获取mv的url
    db.collection('mvURLInfo').where({
      id : id * 1
    })
    .get({
      success:function(res){
        that.setData({
          mvURLInfo:res.data[0]
        })
      }
    })
    
    //获取mv的细节
    db.collection('mvDetail').where({
      id : id * 1
    })
    .get({
      success:function(res){
        that.setData({
          mvDetail:res.data[0]
        })
      }
    })
  
    db.collection('topMVs').where({
      id : id * 1
    })
    .get({
      success:function(res){
        that.setData({
          topMVs:res.data[0]
        })
      }
    })
  }
})